import { is, config } from '../../../lib/myapp.js';
Page({
	data: {
		activeIndex: 0,
		sliderOffset: 0,
		sliderLeft: 0,
		width: 0,
		tabList: [
			{
				title: "获得",
				init: false,
				page: 0,
				more: true,
				show: false
			},
			{
				title: "使用",
				init: false,
				page: 0,
				more: true,
				show: false
			}
		],
		dataList: {
			0: [],
			1: []
		},
		point: {},
		id: ''
	},
	onLoad(e) {
		wx.showLoading({
			title: "加载中",
			mask: true
		});
		if (e.userId) {
			this.setData({
				id: e.userId
			})
			this.queryPoint(e.userId);
			this.queryPointList(this.data.activeIndex);
		}

	},
	onShow() {
		wx.getSystemInfo({
			success: res => {
				this.setData({
					width: res.windowWidth / this.data.tabList.length,
					sliderOffset: res.windowWidth / this.data.tabList.length * this.data.activeIndex
				})
			}
		});
	},
	tabClick(e) {

		var status = e.currentTarget.id;
		this.setData({
			sliderOffset: e.currentTarget.offsetLeft,
			activeIndex: status
		});
		if (!this.data.tabList[status].init) {
			wx.showLoading({
				title: "加载中",
				mask: true
			});

			this.queryPointList(status);
		}
	},
	onReachBottom() {
		var status = this.data.activeIndex
		var page = this.data.tabList[status].page + 1;
		var tabList = `tabList[${status}]`;
		if (this.data.tabList[status].more) {
			this.setData({
				[`${tabList}.show`]: true
			})
			this.queryPointList(status,page);
		} else {
			wx.showToast({
				image: '../../../assets/image/error.png',
				title: '再拉也没有啦'
			});
		}
	},
	// 获取用户积分数据
	queryPoint(id) {
		var token = wx.getStorageSync('user_token');

		wx.request({
			url: config.GLOBAL.baseUrl + "api/O2O/user/point/" + id,
			header: {
				Authorization: token
			},
			success: res => {
				if (res.statusCode == 200) {
					res = res.data;
					if (res.status) {
						this.setData({
							point: res.data
						})
					} else {
						wx.showModal({
							title: '',
							content: res.message,
							showCancel: true
						})
					}
				} else {
					wx.showModal({
						title: '',
						content: '获取用户积分失败',
						showCancel: true
					})
				}
			}
		})
	},
	// 获取积分列表
	queryPointList(status = 0, page = 1) {
		var token = wx.getStorageSync('user_token');
		var data = {
			balance: status ? 'out' : 'in',
			page: page
		};
		wx.request({
			url: config.GLOBAL.baseUrl + "api/O2O/user/point/list/" + this.data.id,
			header: {
				Authorization: token
			},
			data: data,
			success: res => {
				if (res.statusCode) {
					res = res.data;
					if (res.status) {
						var pages = res.meta.pagination;
						var current_page = pages.current_page;
						var total_pages = pages.total_pages;
						var tabList = `tabList[${status}]`;
						this.setData({
							[`dataList[${status}][${page - 1}]`] : res.data,
							[`${tabList}.init`]: true,
							[`${tabList}.page`]: current_page,
							[`${tabList}.more`]: current_page < total_pages,
							[`${tabList}.show`]: false
						})
					} else {
						wx.showToast({
							title: res.message,
							image: '../../../assets/image/error.png'
						})
				}
			} else {
					wx.showModal({
						title: '',
						content: '请求失败',
						showCancel: false
					})}
			},
			complete: ret => {
				wx.hideLoading();
			}
		})

	}
})